<?php

class m110920_021637_requisition extends CDbMigration {
	public function safeUp() {
		$this
			->createTable('erp_material_requisition',
				array(
					'id' => 'pk',
					'projectId' => 'int',
					'submitDate' => 'date',
					'token' => 'string',
					'note' => 'string',
					'approverId' => 'int',
					'warehouseManagerId' => 'int',
					'createTime' => 'datetime',
					'createUserId' => 'int',
					'updateTime' => 'datetime',
					'updateUserId' => 'int',
				));
		$this
			->createIndex("material_requisition_create_user_id", 'erp_material_requisition',
				'createUserId');
		$this
			->addForeignKey('fk_material_requisition_create_user_id', 'erp_material_requisition',
				'createUserId', 'erp_user', 'id', 'SET NULL', 'CASCADE');
		$this
			->createIndex("material_requisition_update_user_id", 'erp_material_requisition',
				'updateUserId');
		$this
			->addForeignKey('fk_material_requisition_update_user_id', 'erp_material_requisition',
				'updateUserId', 'erp_user', 'id', 'SET NULL', 'CASCADE');
		$this
			->createIndex("material_requisition_project_id", 'erp_material_requisition',
				'projectId');
		$this
			->addForeignKey('fk_material_requisition_project_id', 'erp_material_requisition',
				'projectId', 'erp_project', 'id', 'SET NULL', 'CASCADE');
		$this
			->createIndex("material_requisition_approver_id", 'erp_material_requisition',
				'approverId');
		$this
			->addForeignKey('fk_material_requisition_approver_id', 'erp_material_requisition',
				'approverId', 'erp_employee', 'id', 'SET NULL', 'CASCADE');
		$this
			->createIndex("material_requisition_warehouse_manager_id", 'erp_material_requisition',
				'warehouseManagerId');
		$this
			->addForeignKey('fk_material_requisition_warehouse_manager_id',
				'erp_material_requisition', 'warehouseManagerId', 'erp_employee', 'id', 'SET NULL',
				'CASCADE');

		$this
			->createTable('erp_material_requisition_item',
				array(
					'id' => 'pk',
					'materialRequisitionId' => 'int',
					'materialModelId' => 'int',
					'brand' => 'string',
					'amount' => 'decimal(16,5)',
					'unit' => 'string',
					'applicantId' => 'int',
					'amountStandardUnit' => 'decimal(16,5)',
					'note' => 'string',
					'lateSubmit' => 'boolean',
					'createTime' => 'datetime',
					'createUserId' => 'int',
					'updateTime' => 'datetime',
					'updateUserId' => 'int',
				));
		$this
			->createIndex("material_requisition_item_create_user_id",
				'erp_material_requisition_item', 'createUserId');
		$this
			->addForeignKey('fk_material_requisition_item_create_user_id',
				'erp_material_requisition_item', 'createUserId', 'erp_user', 'id', 'SET NULL',
				'CASCADE');
		$this
			->createIndex("material_requisition_item_update_user_id",
				'erp_material_requisition_item', 'updateUserId');
		$this
			->addForeignKey('fk_material_requisition_item_update_user_id',
				'erp_material_requisition_item', 'updateUserId', 'erp_user', 'id', 'SET NULL',
				'CASCADE');
		$this
			->createIndex("material_requisition_item_material_id", 'erp_material_requisition_item',
				'materialModelId');
		$this
			->addForeignKey('fk_material_requisition_item_material_id',
				'erp_material_requisition_item', 'materialModelId', 'erp_material_model', 'id',
				'SET NULL', 'CASCADE');
		$this
			->createIndex("material_requisition_item_applicant_id",
				'erp_material_requisition_item', 'applicantId');
		$this
			->addForeignKey('fk_material_requisition_item_applicant_id',
				'erp_material_requisition_item', 'applicantId', 'erp_employee', 'id', 'SET NULL',
				'CASCADE');

		$this
			->createIndex("material_requisition_item_material_requisition_id",
				'erp_material_requisition_item', 'materialRequisitionId');
		$this
			->addForeignKey('fk_material_requisition_item_material_requisition_id',
				'erp_material_requisition_item', 'materialRequisitionId',
				'erp_material_requisition', 'id', 'CASCADE', 'CASCADE');

		$this
			->insert('erp_employee_tag',
				array(
					'key' => 'MATERIAL_REQUISITION_APPLICANT',
					'name' => '材料申请人',
					'description' => '可以申请购买材料',
				));
		$this
			->insert('erp_employee_tag',
				array(
					'key' => 'MATERIAL_REQUISITION_APPROVER',
					'name' => '材料申购审批人',
					'description' => '可以审批材料申购单',
				));
		$this
			->insert('erp_employee_tag',
				array(
					'key' => 'MATERIAL_WAREHOUSE_MANAGER',
					'name' => '库管',
					'description' => '库管',
				));
	}

	public function safeDown() {
		$this
			->delete('erp_employee_tag', '`key`=:key',
				array(
					':key' => 'MATERIAL_WAREHOUSE_MANAGER',
				));
		$this
			->delete('erp_employee_tag', '`key`=:key',
				array(
					':key' => 'MATERIAL_REQUISITION_APPROVER',
				));
		$this
			->delete('erp_employee_tag', '`key`=:key',
				array(
					':key' => 'MATERIAL_REQUISITION_APPLICANT',
				));
		$this->dropTable('erp_material_requisition_item');
		$this->dropTable('erp_material_requisition');
	}
}
